package com.linkoog.devtools.dubbo.toolwindow;

import com.intellij.icons.AllIcons;
import com.intellij.ide.startup.StartupManagerEx;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.application.ModalityState;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.util.IconLoader;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.openapi.wm.ToolWindowAnchor;
import com.intellij.openapi.wm.ToolWindowManager;
import com.intellij.openapi.wm.ex.ToolWindowEx;
import com.intellij.ui.content.Content;
import com.intellij.ui.content.ContentManager;
import com.intellij.ui.content.impl.ContentImpl;
import com.intellij.util.ModalityUiUtil;
import com.linkoog.devtools.dubbo.raw_index.DubboMethod;
import com.linkoog.devtools.dubbo.toolwindow.environment.DubboTestEnvironmentPanel;
import com.linkoog.devtools.dubbo.toolwindow.workbenche.DubboWorkbencheMainPanel;
import kotlin.Unit;
import org.jetbrains.annotations.NotNull;

import javax.swing.*;

public class DubboToolWindowManager implements Disposable {

    String TOOL_WINDOW_ID = "com.linkoog.devtools.DubboTest";
    String TOOL_WINDOW_NAME = "Dubbo";
    private final Project project;

    private final Key<DubboWorkbencheMainPanel> workbenchePanelKey = Key.create("DubboTest.Key.workbenchePanel");
    private final Key<DubboTestEnvironmentPanel> environmentPanelKey = Key.create("DubboTest.Key.environmentPanel");

    public DubboToolWindowManager(@NotNull Project project) {
        this.project = project;
    }


    public static DubboToolWindowManager getInstance(@NotNull Project project){
        return project.getService(DubboToolWindowManager.class);
    }

    @Override
    public void dispose() {
    }

    public @NotNull ToolWindowEx getOrCreateToolWindow() {
        ToolWindowManager toolWindowManager = ToolWindowManager.getInstance(project);
        ToolWindowEx toolWindowEx = (ToolWindowEx)toolWindowManager.getToolWindow(TOOL_WINDOW_ID);
        if (toolWindowEx != null) {
            return toolWindowEx;
        }

        toolWindowEx = (ToolWindowEx)toolWindowManager.registerToolWindow(TOOL_WINDOW_ID, builder -> {
            builder.stripeTitle = () -> TOOL_WINDOW_NAME;
            builder.icon = IconLoader.getIcon("/icons/dubbo/dubbo1.svg", DubboToolWindowManager.class);
            builder.canCloseContent = false;
            builder.hideOnEmptyContent = true;
            builder.anchor = ToolWindowAnchor.BOTTOM;
            builder.sideTool = false;
            return Unit.INSTANCE;
        });
        toolWindowEx.setAutoHide(false);
        createDubboTestContent(toolWindowEx);
        createDubboSettingContent(toolWindowEx);


        ToolWindowEx finalToolWindowEx = toolWindowEx;
        AnAction closeToolWindowAction = new AnAction("close", "close", AllIcons.Actions.Close){
            @Override
            public void actionPerformed(@NotNull AnActionEvent e) {
                Project project = e.getProject();
                if (project == null) return;
                finalToolWindowEx.remove();
            }
        };
        toolWindowEx.setTabActions(closeToolWindowAction);


        return toolWindowEx;
    }


    private void createDubboTestContent(ToolWindowEx toolWindowEx){
        final ContentManager contentManager = toolWindowEx.getContentManager();

        DubboWorkbencheMainPanel dubboWorkbencheMainPanel = new DubboWorkbencheMainPanel(project);
        ContentImpl content = new ContentImpl(dubboWorkbencheMainPanel.getMyMainPanel(), "Workbenche", false);
        content.setIcon(AllIcons.General.RunWithCoverage);
        content.putUserData(workbenchePanelKey, dubboWorkbencheMainPanel);
        content.putUserData(ToolWindow.SHOW_CONTENT_ICON, Boolean.TRUE);
        Disposer.register(content, dubboWorkbencheMainPanel);
        contentManager.addContent(content);
    }

    private void createDubboSettingContent(ToolWindowEx toolWindowEx){
        final ContentManager contentManager = toolWindowEx.getContentManager();

        DubboTestEnvironmentPanel dubboTestEnvironmentPanel = new DubboTestEnvironmentPanel(project);
        ContentImpl content = new ContentImpl(dubboTestEnvironmentPanel.getMyMainPanel(), "Environment", false);
        content.setIcon(AllIcons.General.Settings);
        content.putUserData(environmentPanelKey, dubboTestEnvironmentPanel);
        content.putUserData(ToolWindow.SHOW_CONTENT_ICON, Boolean.TRUE);
        contentManager.addContent(content);

    }

    public void show(DubboMethod dubboMethod){
        SwingUtilities.invokeLater(() -> {
            ToolWindowEx toolWindowEx = getOrCreateToolWindow();
            ContentManager contentManager = toolWindowEx.getContentManager();
            Content dubboWorkbencheContent = contentManager.getContent(0);
            DubboWorkbencheMainPanel dubboWorkbencheMainPanel = dubboWorkbencheContent.getUserData(workbenchePanelKey);
            dubboWorkbencheMainPanel.resetPanel(dubboMethod);
            contentManager.setSelectedContent(dubboWorkbencheContent,false);
            toolWindowEx.show();
        });
    }

    public void removeContent(Content content) {
        ToolWindow toolWindow = ToolWindowManager.getInstance(project).getToolWindow(TOOL_WINDOW_ID);
        ContentManager contentManager = toolWindow == null ? null : toolWindow.getContentManager();
        if (contentManager != null && !contentManager.isDisposed()) {
            contentManager.removeContent(content, true);
        }
    }


}
